﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using IBatisNet.Common.Exceptions;
using IBatisNet.Common.Pagination;
using IBatisNet.DataAccess;
using IBatisNet.DataAccess.DaoSessionHandlers;
using IBatisNet.DataAccess.Interfaces;
using IBatisNet.DataMapper;

namespace Wolfut.MirageEE.Core.DAO
{
   public class BaseSqlMapDao :BaseDao
    {
       protected const int PAGE_SIZE = 4;
       /// <summary>
       /// Looks up the parent DaoManager, gets the local transaction
       /// (which should be a SqlMapDaoTransaction) and returns the
       /// SqlMap associated with this DAO.
       /// </summary>
       /// <returns>The SqlMap instance for this DAO.</returns>
       protected ISqlMapper GetLocalSqlMap()
       {
           SqlMapDaoSession session = (SqlMapDaoSession)this.GetContext();
           return session.SqlMap;
       }

       /// <summary>
       /// Simple convenience method to wrap the SqlMap method of the same name.
       /// Wraps the exception with a IBatisNetException to isolate the SqlMap framework.
       /// </summary>
       /// <param name="statementName"></param>
       /// <param name="parameterObject"></param>
       /// <returns></returns>
       protected IList ExecuteQueryForList(string statementName, object parameterObject)
       {
           ISqlMapper sqlMap = GetLocalSqlMap();
           try
           {
               return sqlMap.QueryForList(statementName, parameterObject);
               
           }
           catch (Exception e)
           {
               throw new IBatisNetException("Error executing query '" + statementName + "' for list.  Cause( " + e.Message, e);
           }
       }


       protected IList<T> ExecuteQueryForList<T>(string statementName, object parameterObject) where T:class 
       {
           ISqlMapper sqlMap = GetLocalSqlMap();
           try
           {
               return sqlMap.QueryForList<T>(statementName, parameterObject);

           }
           catch (Exception e)
           {
               throw new IBatisNetException("Error executing query '" + statementName + "' for list.  Cause( " + e.Message, e);
           }
       }

       /// <summary>
       /// Simple convenience method to wrap the SqlMap method of the same name.
       /// Wraps the exception with a IBatisNetException to isolate the SqlMap framework.
       /// </summary>
       /// <param name="statementName"></param>
       /// <param name="parameterObject"></param>
       /// <param name="skipResults"></param>
       /// <param name="maxResults"></param>
       /// <returns></returns>
       protected IList ExecuteQueryForList(string statementName, object parameterObject, int skipResults, int maxResults)
       {
           ISqlMapper sqlMap = GetLocalSqlMap();
           try
           {
               return sqlMap.QueryForList(statementName, parameterObject, skipResults, maxResults);
           }
           catch (Exception e)
           {
               throw new IBatisNetException("Error executing query '" + statementName + "' for list.  Cause: " + e.Message, e);
           }
       }

       protected IList<T> ExecuteQueryForList<T>(string statementName, object parameterObject, int skipResults, int maxResults)
       {
           ISqlMapper sqlMap = GetLocalSqlMap();
           try
           {
               return sqlMap.QueryForList<T>(statementName, parameterObject, skipResults, maxResults);
           }
           catch (Exception e)
           {
               throw new IBatisNetException("Error executing query '" + statementName + "' for list.  Cause: " + e.Message, e);
           }
       }

       /// <summary>
       /// Simple convenience method to wrap the SqlMap method of the same name.
       /// Wraps the exception with a IBatisNetException to isolate the SqlMap framework.
       /// </summary>
       /// <param name="statementName"></param>
       /// <param name="parameterObject"></param>
       /// <returns></returns>
       protected object ExecuteQueryForObject(string statementName, object parameterObject)
       {
           ISqlMapper sqlMap = GetLocalSqlMap();

           try
           {
               return sqlMap.QueryForObject(statementName, parameterObject);
           }
           catch (Exception e)
           {
               throw new IBatisNetException("Error executing query '" + statementName + "' for object.  Cause: " + e.Message, e);
           }
       }

       protected T ExecuteQueryForObject<T>(string statementName, object parameterObject)
       {
           ISqlMapper sqlMap = GetLocalSqlMap();

           try
           {
               return sqlMap.QueryForObject<T>(statementName, parameterObject);
           }
           catch (Exception e)
           {
               throw new IBatisNetException("Error executing query '" + statementName + "' for object.  Cause: " + e.Message, e);
           }
       }

       /// <summary>
       /// Simple convenience method to wrap the SqlMap method of the same name.
       /// Wraps the exception with a IBatisNetException to isolate the SqlMap framework.
       /// </summary>
       /// <param name="statementName"></param>
       /// <param name="parameterObject"></param>
       /// <returns></returns>
       protected int ExecuteUpdate(string statementName, object parameterObject)
       {
           ISqlMapper sqlMap = GetLocalSqlMap();

           try
           {
               return sqlMap.Update(statementName, parameterObject);
           }
           catch (Exception e)
           {
               throw new IBatisNetException("Error executing query '" + statementName + "' for update.  Cause: " + e.Message, e);
           }
       }

       /// <summary>
       /// Simple convenience method to wrap the SqlMap method of the same name.
       /// Wraps the exception with a IBatisNetException to isolate the SqlMap framework.
       /// </summary>
       /// <param name="statementName"></param>
       /// <param name="parameterObject"></param>
       /// <returns></returns>
       protected object ExecuteInsert(string statementName, object parameterObject)
       {
           ISqlMapper sqlMap = GetLocalSqlMap();

           try
           {
               return sqlMap.Insert(statementName, parameterObject);
           }
           catch (Exception e)
           {
               throw new IBatisNetException("Error executing query '" + statementName + "' for insert.  Cause: " + e.Message, e);
           }
       }

       /// <summary>
       /// Simple convenience method to wrap the SqlMap method of the same name.
       /// Wraps the exception with a IBatisNetException to isolate the SqlMap framework.
       /// </summary>
       /// <param name="statementName"></param>
       /// <param name="parameterObject"></param>
       /// <returns></returns>
       protected object ExecuteDelete(string statementName, object parameterObject)
       {
           ISqlMapper sqlMap = GetLocalSqlMap();

           try
           {
               return sqlMap.Delete(statementName, parameterObject);
           }
           catch (Exception e)
           {
               throw new IBatisNetException("Error executing query '" + statementName + "' for delete.  Cause: " + e.Message, e);
           }
       }
    }
}
